package main

import (
"sync"
"fmt"
"time"
)

type Account struct {
	money int
	Mt    sync.Mutex
}

func (a *Account) SaveMoney(amount int) int {
	if a.CheckPassword() {
		a.Mt.Lock()
		fmt.Println("开始存钱...")
		//<-time.NewTimer(10 * time.Second).C
		<-time.After(10 * time.Second)
		a.money += amount
		fmt.Println("存钱结束")
		a.Mt.Unlock()
		return a.money
	}
	return -1
}
func (a *Account) GetMoney(amount int) int {
	if a.CheckPassword() {
		a.Mt.Lock()
		fmt.Println("开始取钱...")
		//<-time.NewTimer(10 * time.Second).C
		<-time.After(10 * time.Second)
		a.money -= amount
		fmt.Println("取钱结束")
		a.Mt.Unlock()
		return a.money
	}
	return -1
}
func (a *Account) PrintHistory() {
	if a.CheckPassword() {
		fmt.Println("流水已打印！")
	}
}
func (a *Account) Query() {
	if a.CheckPassword() {
		fmt.Println("您的余额为", a.money)
	}
}
func (a *Account) CheckPassword() bool {
	//var password string
	//fmt.Println("骚年请输入密码：")
	//fmt.Scan(&password)
	//if password == "123456" {
	//	//通过密码校验
	//	fmt.Println("登录成功！")
	//	return true
	//} else {
	//	//未通过密码校验
	//	fmt.Println("尊敬的用户，密码错误，fuck off!")
	//	return false
	//}
	return true
}

func main() {
	account := &Account{money: 1000}
	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		account.SaveMoney(500)
		wg.Done()
	}()

	wg.Add(1)
	go func() {
		account.GetMoney(500)
		wg.Done()
	}()

	wg.Add(1)
	go func() {
		account.PrintHistory()
		wg.Done()
	}()

	wg.Add(1)
	go func() {
		account.Query()
		wg.Done()
	}()

	wg.Wait()
	fmt.Println("THE END")

}
